home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / FIX_DESK / SOURCE / VOLUME.C < prev   
C/C++ Source or Header  |  1988-09-07  |  4KB  |  164 lines

  1. #include <MacTypes.h>
  2. #include <QuickDraw.h>
  3. #include <WindowMgr.h>
  4. #include <DialogMgr.h>
  5. #include <EventMgr.h>
  6. #include <FileMgr.h>
  7. #include <HFS.h>
  8.  
  9. #include "fix.h"
  10.  
  11. #define RETURN_KEY 0x24
  12. #define TAB_KEY    0x30
  13. #define ENTER_KEY  0x34
  14.  
  15. enum {
  16.     ITEM_VOLUME_OK = 1,
  17.     ITEM_VOLUME_CANCEL,
  18.     ITEM_VOLUME_VOLNAME = 4,
  19.     ITEM_VOLUME_DRIVE,
  20.     ITEM_VOLUME_EJECT
  21. };
  22.  
  23. int get_volume_info(int index, int *refnum, unsigned char *name, int *drive,
  24.                     int *is_hfs);
  25. pascal Boolean disk_filter(DialogPtr the_dialog, EventRecord *the_event,
  26.                            int *item_hit);
  27.  
  28.     /* Volume selection and utilities. */
  29.  
  30.     /* Select which volume to process. */
  31.  
  32. int select_volume_dialog(refnum, is_hfs) /* Return 1 if OK, 0 if canceled */
  33.     int *refnum, *is_hfs;
  34. {
  35.     register DialogPtr the_dialog;
  36.     register Handle the_item;
  37.     int item, drive;
  38.     register int current_index, index_changed, done;
  39.     unsigned char volume_name[NAME_SIZE];
  40.  
  41.     the_dialog = GetNewDialog(DIALOG_VOLUME, NIL, MINUS_ONE);
  42.     the_item = get_item(the_dialog, ITEM_VOLUME_VOLNAME);
  43.  
  44.     index_changed = current_index = 1;
  45.     done = 0;
  46.     do {
  47.         if (index_changed) {
  48.             register int err;
  49.  
  50.             index_changed = 0;
  51.             err = get_volume_info(current_index, refnum, volume_name, &drive,
  52.                                   is_hfs);
  53.             while (!err && !drive) {    /* List only on-line volumes... */
  54.                 current_index++;
  55.                 err = get_volume_info(current_index, refnum, volume_name,
  56.                                       &drive,is_hfs);
  57.             }
  58.             if (err)    /* If at end of list, just use first volume. */
  59.                 get_volume_info(current_index = 1, refnum, volume_name,
  60.                                 &drive, is_hfs);
  61.             SetIText(the_item, volume_name);
  62.         }
  63.         ModalDialog(disk_filter, &item);
  64.         if (item < 0) {                /* Disk inserted: try to find it. */
  65.             register int found;
  66.  
  67.             item = -item;
  68.             current_index = 1;
  69.             found = 0;
  70.             while (!found) {
  71.                 if (get_volume_info(current_index, refnum, volume_name, &drive,
  72.                                     is_hfs) != 0)
  73.                     found = current_index = 1;        /* Abort on error. */
  74.                 else
  75.                     if (drive == item)                /* We found it! */
  76.                         found = 1;
  77.                     else
  78.                         current_index++;
  79.             }
  80.             index_changed = 1;
  81.         }
  82.         else
  83.             switch (item) {
  84.                 case ITEM_VOLUME_OK:
  85.                 case ITEM_VOLUME_CANCEL:
  86.                     done = 1;
  87.                     break;
  88.                 case ITEM_VOLUME_DRIVE:
  89.                     current_index++;
  90.                     index_changed = 1;
  91.                     break;
  92.                 case ITEM_VOLUME_EJECT:
  93.                     Eject(NIL, *refnum);
  94.                     index_changed = 1;
  95.                     break;
  96.             }
  97.     } while (!done);
  98.     DisposDialog(the_dialog);
  99.  
  100.     if (item == ITEM_VOLUME_OK) {
  101.         ParamText(volume_name, NIL, NIL, NIL);    /* Set volume name */
  102.         return(1);
  103.     }
  104.     else
  105.         return(0);
  106. }
  107.  
  108.     /* Get information about a volume, given its index. */
  109.  
  110. int get_volume_info(index, refnum, name, drive, is_hfs)
  111.     int index, *refnum, *drive, *is_hfs;
  112.     unsigned char *name;
  113. {
  114.     register int err;
  115.     HParamBlockRec blob;
  116.  
  117.     blob.volumeParam.ioCompletion = NIL;
  118.     blob.volumeParam.ioNamePtr = name;
  119.     blob.volumeParam.ioVolIndex = index;
  120.     err = PBHGetVInfo(&blob, 0);
  121.  
  122.     if (!err) {
  123.         *refnum = blob.volumeParam.ioVRefNum;
  124.         *drive = blob.volumeParam.ioVDrvInfo;
  125.         *is_hfs = (blob.volumeParam.ioVSigWord == 'BD');
  126.     }
  127.     return(err);
  128. }
  129.  
  130.     /* Filter to allow mounting of volumes within a dialog. */
  131.  
  132. static pascal Boolean disk_filter(the_dialog, the_event, item_hit)
  133.     DialogPtr the_dialog;
  134.     register EventRecord *the_event;
  135.     register int *item_hit;
  136. {
  137.     if (the_event->what == keyDown) {
  138.         register int key_code;
  139.  
  140.         key_code = (the_event->message >> 8) & 0xFF;    /* Keyboard key */
  141.         if ((key_code == RETURN_KEY) || (key_code == ENTER_KEY)) {
  142.             *item_hit = ITEM_VOLUME_OK;
  143.             return (TRUE);
  144.         }
  145.         else if (key_code == TAB_KEY) {
  146.             *item_hit = ITEM_VOLUME_DRIVE;
  147.             return (TRUE);
  148.         }
  149.     }
  150.     else if (the_event->what == nullEvent) {
  151.         EventRecord disk_event;
  152.  
  153.         if (GetNextEvent(diskMask, &disk_event))
  154.             if (HiWord(disk_event.message))
  155.                 Eject(NIL, LoWord(disk_event.message));
  156.             else {
  157.                 *item_hit = -LoWord(disk_event.message);
  158.                 return (TRUE);
  159.             }
  160.     };
  161.  
  162.     return (FALSE);
  163. }
  164.